<?php

namespace app\services\data\webscript;

use app\services\data\stat\AdStatService;
use app\services\ServiceBase;

class AdStatScriptService extends ServiceBase
{
    public function statDataGetReport($params)
    {
        $data = AdStatService::getInstance()->statReport($params);
        $data['keys'] = array_merge($data['groupbys'], array_keys($data['total']));

        return $data;
    }

    public function statDataGet($params, $stat_type = '')
    {
        switch ($stat_type) {
            case 'base':
                $stats = ['firstup', 'reg', 'login', 'role_reg'];
                break;
            case 'pay':
                $stats = ['pay', 'regpay', 'pay_noe'];
                break;
            default:
            $stats = ['firstup', 'reg', 'login', 'role_reg', 'pay', 'regpay', 'pay_noe'];
                break;
        }

        $data = AdStatService::getInstance()->statData($params, $stats);

        $data['keys'] = array_merge($data['groupbys'], array_keys($data['total']));

        return $data;
    }

    public function statDataGetXuni($params)
    {
        $data = AdStatService::getInstance()->statData($params, ['role_reg_new', 'toapk_reg', 'toapk_reg_all', 'postback_reg']);

        $data['keys'] = array_merge($data['groupbys'], array_keys($data['total']));

        return $data;
    }

    // 获取米大师数据
    public function statDataGetMidas($params)
    {
        $data = AdStatService::getInstance()->statData($params, ['pay_midas', 'regpay_midas']);

        $data['keys'] = array_merge($data['groupbys'], array_keys($data['total']));

        return $data;
    }

    public function statDataGetKeep($params)
    {
        $data = AdStatService::getInstance()->statKeep($params);

        $prefix_list = [
            1 => 'day',
            2 => 'pay_day',
            5 => 'pay_one',
            6 => 'pay_old',
            7 => 'old_day',
        ];

        $keep_type = $params['keep_type'] ?? 1;
        $prefix = $prefix_list[$keep_type];
        $keep_style = $params['keep_style'] ?? 'keep';
        $keep_value = $params['keep_value'] ?? 'per';

        // 字段
        $keys = $data['groupbys'];
        foreach ($data['days'] as $day) {
            $keys[] = "{$prefix}{$day}";
        }

        // 汇总
        $total = [];
        foreach ($data['days'] as $dayid => $day) {
            $key      = "{$prefix}{$day}";
            $dayindex = $dayid+1;
            $reg      = $data['total'][$dayindex]['reg'];
            $login    = $data['total'][$dayindex]['login'];
            // 按概率显示
            if ($keep_value == 'per') {
                if ($keep_style == 'keep') {
                    $per = $reg == 0?0:round($login/$reg*100, 2);
                } else {
                    $per = $reg == 0?0:round(($reg-$login)/$reg*100, 2);
                }
                $total[$key] = $per>0?"{$per}%":"0.00%";
            } else {
                // 按数值显示
                if ($keep_style == 'keep') {
                    $per = $login;
                } else {
                    $per = $reg-$login;
                }
                $total[$key] = $per;
            }
        }

        // 每一天流程
        $list = [];
        foreach ($data['list'] as $index => $keep_val) {
            foreach ($data['groupbys'] as $key) {
                $list[$index][$key] = $keep_val[$key];
            }

            $keeps = $keep_val['keeps'];
            foreach ($data['days'] as $dayid => $day) {
                $key      = "{$prefix}{$day}";
                $dayindex = $dayid+1;
                $reg      = $keeps[$dayindex]['reg'];
                $login    = $keeps[$dayindex]['login'];
                // 按概率显示
                if ($keep_value == 'per') {
                    if ($keep_style == 'keep') {
                        $per = $reg == 0?0:round($login/$reg*100, 2);
                    } else {
                        $per = $reg == 0?0:round(($reg-$login)/$reg*100, 2);
                    }
                    $list[$index][$key] = $per>0?"{$per}%":"0.00%";
                } else {
                    // 按数值显示
                    if ($keep_style == 'keep') {
                        $per = $login;
                    } else {
                        $per = $reg-$login;
                    }
                    $list[$index][$key] = $per;
                }
            }
        }

        return [
            'keys' => $keys,
            'total' => $total,
            'allsum' => $total,
            'list' => $list,
        ];
    }

    public function statDataGetLtv($params)
    {
        $data       = AdStatService::getInstance()->statLtv($params);
        $data_list  = $data['list'];
        $data_total = $data['total'];
        $template   = $data['template'];

        // 补齐ltv每天的注册数
        $reg = AdStatService::getInstance()->statData($params, ['reg']);
        foreach ($reg['list'] as $index => $val) {
            if (!isset($data_list[$index])) {
                $data_list[$index] = array_merge($val, $template);
            }
            $data_list[$index]['reg'] = $val['reg'];
        }
        $data_total['reg'] = $reg['total']['reg'];

        // 字段
        $keys = $data['groupbys'];
        for ($day=1; $day <= $data['ltv']; $day++) {
            $keys[] = "ltv{$day}";
        }

        // 汇总
        $total = [];
        for ($day=1; $day <= $data['ltv']; $day++) {
            $key         = "ltv{$day}";
            $pay_money   = $data_total[$day]['pay_money'];
            $per         = $data_total['reg'] == 0?0:round($pay_money/$data_total['reg'], 2);
            $total[$key] = $per;
        }

        // 每一天流程
        $list = [];
        foreach ($data_list as $index => $ltv_val) {
            foreach ($data['groupbys'] as $key) {
                $list[$index][$key] = $ltv_val[$key];
            }

            $ltvs = $ltv_val['ltvs'];
            for ($day=1; $day <= $data['ltv']; $day++) {
                $key                = "ltv{$day}";
                $pay_money          = $ltvs[$day]['pay_money'];
                $per                = $ltv_val['reg'] == 0?'0.00':round($pay_money/$ltv_val['reg'], 2);
                $list[$index][$key] = $per;
            }
        }

        return [
            'keys' => $keys,
            'total' => $total,
            'allsum' => $total,
            'list' => $list,
        ];
    }

    public function statDataGetRoi($params)
    {
        $data = AdStatService::getInstance()->statRoi($params);
        $keys = $data['groupbys'];

        return [
            'keys'   => $keys,
            'total'  => $data['total'],
            'allsum' => $data['total'],
            'list'   => $data['list'],
        ];
    }

    public function statDataGetRoiMidas($params)
    {
        $data = AdStatService::getInstance()->statRoiMidas($params);
        $keys = $data['groupbys'];

        return [
            'keys'   => $keys,
            'total'  => $data['total'],
            'allsum' => $data['total'],
            'list'   => $data['list'],
        ];
    }
}
